package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import figure8.Controller;
import figure8.Track;
import figure8.WAVTrack;

public class WAVTrackSettingsPanel extends JPanel {
	private JLabel name;
	
	public WAVTrackSettingsPanel(final Controller controller, final Track track) {
		super();
		
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(230, 150));
		final WAVTrackSettingsPanel settingsWin = this;
		
      //NAME
		this.name = new JLabel(track.getName(), JLabel.CENTER);
		this.name.addMouseListener(new MouseListener() {
			
			public void mouseClicked(MouseEvent arg0) {	}

			public void mouseEntered(MouseEvent arg0) {	}

			public void mouseExited(MouseEvent arg0) { }

			public void mousePressed(MouseEvent arg0) {
				ChangeTrackNameWindow change = new ChangeTrackNameWindow(
						controller, (WAVTrack) track, settingsWin);
			}

			public void mouseReleased(MouseEvent arg0) { }

		});
		this.add(name, BorderLayout.PAGE_START);
	
      //ROW 1
		JPanel row1Panel = new JPanel();
		row1Panel.setLayout(new BoxLayout(row1Panel, BoxLayout.X_AXIS));
		
        //VOLUME
		row1Panel.add(new JLabel(" Vol"));
		final JSlider volume = new JSlider(-30, 14, (int) track.getVolume());
		row1Panel.add(volume);
		
		volume.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				track.setVolume(volume.getValue());
			}
		});
		
		//MUTE
		final JToggleButton mute = new JToggleButton("M");
		row1Panel.add(mute, BorderLayout.LINE_END);

		mute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				track.setMuted(!track.getMuted());
			}
		});
		
		//SOLO
		final JToggleButton solo = new JToggleButton("S");
		row1Panel.add(solo, BorderLayout.LINE_END);

		solo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				track.setSoloing(!track.getSoloing());
				controller.soloTrack(track);
			}
		});
		
		this.add(row1Panel, BorderLayout.CENTER);

      //ROW 2
		JPanel row2Panel = new JPanel();
		row2Panel.setLayout(new BoxLayout(row2Panel, BoxLayout.X_AXIS));

	    //PAN		
		row2Panel.add(new JLabel(" Pan"));
		final JSlider pan = new JSlider(-1, 1, (int) track.getPan());
		row2Panel.add(pan);
		
		pan.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				track.setPan(pan.getValue());
			}
		});
		
		//FX
		final JToggleButton effect = new JToggleButton("fx");
		row2Panel.add(effect, BorderLayout.LINE_END);
		
		effect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.stop();
				controller.getWindow().getControlPanel().stopped();
				EffectWindow effWindow = new EffectWindow(
				controller, (WAVTrack) track, settingsWin);
			}
		});
		
		//DELETE
		final JButton delete = new JButton("D");
		row2Panel.add(delete, BorderLayout.LINE_END);
		
		delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.deleteTrack(track);
			}
		});

		this.add(row2Panel, BorderLayout.AFTER_LAST_LINE);
	}

	public void setLabel(String text) {
		this.name.setText(text);
	}
}
